using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ProgrammingEducationSystem.Models;
using ProgrammingEducationSystem.Services.IServices;
using ProgrammingEducationSystem.Repository.IRepository;

namespace ProgrammingEducationSystem.Services
{
    public class LearnSourcesService : ILearnSourcesService
    {
        private readonly ILearnSourceRepository _learnSourceRepository;
        private readonly IClassRepository _classRepository;
        private readonly ICourseHourRepository _courseHourRepository;
        public LearnSourcesService(ILearnSourceRepository learnSourceRepository,IClassRepository classRepository,ICourseHourRepository courseHourRepository)
        {
            _learnSourceRepository=learnSourceRepository;
            _classRepository=classRepository;
            _courseHourRepository=courseHourRepository;
        }
        public async Task AddAsync(LearnSource learnSource)
        {
            await _learnSourceRepository.AddAsync(learnSource);
        }

        public async Task<List<LearnSource>?> AllAsync()
        {
            return await _learnSourceRepository.FindAllAsync();
        }

        public async Task Delete(Guid id)
        {
            var learnSource =await _learnSourceRepository.FindAsync(id);
            if (learnSource != null)
            {
                _learnSourceRepository.Delete(learnSource);
            }
        }

        public async Task DeleteByCourseTypeIdList(List<Guid> courseTypeIdList)
        {
            var learnSource =await _learnSourceRepository.GetByCourseTypeIdList(courseTypeIdList);
            if (learnSource != null)
            {
                _learnSourceRepository.DeleteList(learnSource);
            }
        }

        public async Task<List<LearnSource>?> GetByCourseType(string courseTypeName)
        {
            return await _learnSourceRepository.GetByCourseType(courseTypeName);
        }

        public async Task<List<LearnSource>?> GetByCourseType(Guid courseTypeId)
        {
            return await _learnSourceRepository.GetByCourseType(courseTypeId);
        }

        public async Task<LearnSource?> GetByIdAsync(Guid id)
        {
            return await _learnSourceRepository.FindAsync(id);
        }



        public async Task<bool> SaveAsync()
        {
            return await _classRepository.SaveAsync();
        }

        public void Update(LearnSource learnSource)
        {
            _learnSourceRepository.Update(learnSource);
        }


    }
}


//以下代码由于模型修改而被废弃

////由于外键修改，该方法被废弃
//public async Task<List<LearnSource>?> GetByTeacherIdAsync(string id)
//{
//    return await _learnSourceRepository.GetByTeacherIdAsync(id);
//}



////由于外键修改，该方法被废弃
//public async void DeleteByClassId(Guid id)
//{
//    var learnSources = await _learnSourceRepository.GetByClassIdAsync(id);
//    if (learnSources != null && learnSources.Any())
//    {
//        _learnSourceRepository.DeleteList(learnSources);
//    }
//}


////由于外键修改，该方法被废弃
//public async void DeleteByCourseId(Guid id)
//{
//    var learnSources = await _learnSourceRepository.GetByCourseHourIdAsync(id);
//    if (learnSources != null && learnSources.Any())
//    {
//        foreach (var learnSource in learnSources)
//        {
//            _learnSourceRepository.Delete(learnSource);
//        }
//    }
//}


//由于外键修改，该方法被废弃
//public async void DeleteByTeacherId(string id)
//{
//    var learnSources = await _learnSourceRepository.GetByTeacherIdAsync(id);
//    if (learnSources != null && learnSources.Any())
//    {
//        foreach (var learnSource in learnSources)
//        {
//             _learnSourceRepository.Delete(learnSource);
//        }
//    }
//}

//由于外键修改，该方法被废弃
//public async Task<List<LearnSource>?> GetByClassIdAsync(Guid id)
//{
//    return await _learnSourceRepository.GetByClassIdAsync(id);
//}

////由于外键修改，该方法被废弃
//public async Task<List<LearnSource>?> GetByCourseHourIdAsync(Guid id)
//{
//    return await _learnSourceRepository.GetByCourseHourIdAsync(id);
//}